import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        //1.定义商品类
        //2.定义购物车对象，因为目前只有数组学过，其实应该用集合
        Goods[] shopCar = new Goods[100];
        Scanner sc = new Scanner(System.in);
        //3.操作
        OUT:
        while (true) {
            System.out.println("==========================");
            System.out.println("添加商品:add");
            System.out.println("查询商品:query");
            System.out.println("修改商品购买数量:update");
            System.out.println("结算:pay");
            System.out.println("退出:quit");
            System.out.println("输入命令操作:");
            String operation = sc.next();
            switch (operation) {
                case "add":
                    addGoods(shopCar, sc);
                    break;
                case "query":
                    queryGoods(shopCar);
                    break;
                case "update":
                    updateBuyNumber(shopCar, sc);
                    break;
                case "pay":
                    pay(shopCar);
                    break;
                case "quit":
                    break OUT;
                default:
                    System.out.println("命令输入有错!");
            }
        }
    }

    public static void addGoods(Goods[] shopCar, Scanner sc) {
        Goods watch = new Goods();
        System.out.println("输入商品id:");
        int id = sc.nextInt();
        System.out.println("输入商品名称:");
        String name = sc.next();
        System.out.println("输入商品价格:");
        double price = sc.nextDouble();
        System.out.println("输入商品数量:");
        int buyNumber = sc.nextInt();
        watch.buyNumber = buyNumber;
        watch.price = price;
        watch.id = id;
        watch.name = name;
        for (int i = 0; i < shopCar.length; i++) {
            if (shopCar[i] == null) {
                // 此位置没有元素存入,存入后需要break
                shopCar[i] = watch;
                break;
            }
        }
        System.out.println(watch.name + "已经添加!");
    }

    public static void queryGoods(Goods[] shopCar) {
        for (int i = 0; i < shopCar.length; i++) {
            if (shopCar[i] != null) {
                System.out.println("Id:"+ shopCar[i].id +"\t"+"Name:"+shopCar[i].name);
            } else {
                break;
            }
        }
    }

    public static void updateBuyNumber(Goods[] shopCar, Scanner sc) {
        System.out.println("输入要修改的id:");
        int id = sc.nextInt();
        System.out.println("输入修改后的商品名称:");
        String name = sc.next();
        System.out.println("输入修改后的商品价格:");
        double price = sc.nextDouble();
        System.out.println("输入修改后的商品数量:");
        int buyNumber = sc.nextInt();
        boolean flag = false;  // true代表存在
        for (int i = 0; i < shopCar.length; i++) {
            if (shopCar[i]!=null && shopCar[i].id == id){
                shopCar[i].price = price;
                shopCar[i].name = name;
                shopCar[i].buyNumber = buyNumber;
                flag = true;
                break;
            }
        }
        if(!flag){
            System.out.println("Id不存在");
        }

    }

    public static void pay(Goods[] shopCar) {
        double sum = 0.0;
        try {

            for (int i = 0; i < shopCar.length; i++) {
                if (shopCar[i] != null) {
                    sum += (shopCar[i].price * shopCar[i].buyNumber);
                } else {
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("总价格:" + sum);
    }
}
